package com.gitee.huanminabc.utils_tools.log_extend.restTemplate;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 * @author 胡安民
 * 使用 bean后置处理器 扩展 RestTemplate对链路的支持
 */
@Component
public class LogTraceRestTemplatePostProcessor implements InstantiationAwareBeanPostProcessor {

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (bean instanceof RestTemplate){
            RestTemplate restTemplate = (RestTemplate) bean;
            RestTemplateLogTraceInterceptor restTemplateLogTraceInterceptor = new RestTemplateLogTraceInterceptor();
            restTemplate.setInterceptors(Collections.singletonList(restTemplateLogTraceInterceptor));
        }
        return true;
    }
}
